<?php
namespace App\Repositories\Finance;

use App\Models\Finance\FinanceRecordModel;
use App\Repositories\BaseRepository;

class FinanceRecordRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new FinanceRecordModel();
    }

    public function getAdminIndex()
    {
        $models = $this->repository->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 当前用户平台收益
     * @param int $page
     * @return mixed
     */
    public function getApiIndex($uid,$page=1)
    {
        $pageLimit = 10;
        $pageStart = ($page - 1) * $pageLimit;
        $models = $this->repository
                ->where('receiver',$uid)
                ->orderBy('id','desc')
                ->skip($pageStart)
                ->take($pageLimit)
                ->get();
        return $models;
    }

    /**
     * 平台内部支付
     *
     */
    public function setAdd($data)
    {
        if (in_array($data['genre'],[1,2,3]) ) {
            $data['status'] = 2; //订单支付
        } else {
            $data['status'] = 3; //其他：红包、工资
        }
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$orderId)
    {
        $data['updated_at'] = time();
        return $this->repository->where('bale',$orderId)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    public function getOneByOrder($orderId,$status=0)
    {
        $query = $this->repository
            ->where('order_id',$orderId);
        if ($status) { $query = $query->where('status',$status); }
        return $query->first();
    }
}